﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Quick.PluginFramework.ViewEngines
{
    public class ThemeableRazorViewEngine : ThemeableBuildManagerViewEngine
    {
        public ThemeableRazorViewEngine()
        {
            PluginViewLocationFormats = new[] {
                "~/Plugins/{3}/{2}/Views/{1}/{0}.cshtml",
                "~/Plugins/{3}/{2}/Views/Shared/{0}.cshtml",};

            PluginMasterLocationFormats = new[] {
                "~/Plugins/{2}/Views/Shared/Themes/{3}/{0}.cshtml",
                "~/Plugins/{2}/Views/{1}/{0}.cshtml",
                "~/Plugins/{2}/Views/Shared/{0}.cshtml",
            };

            PluginPartialViewLocationFormats = new[] {
                "~/Plugins/{2}/Views/Shared/Themes/{3}/{0}.cshtml",
                "~/Plugins/{2}/Views/{1}/{0}.cshtml",
                "~/Plugins/{2}/Views/Shared/{0}.cshtml",
            };

            AreaViewLocationFormats = new[] {
                "~/Areas/{2}/Views/Shared/Themes/{3}/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml",
            };

            AreaMasterLocationFormats = new[] {

                "~/Areas/{2}/Views/Shared/Themes/{3}/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml",
            };

            AreaPartialViewLocationFormats = new[] {
                "~/Areas/{2}/Views/Shared/Themes/{3}/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                "~/Areas/{2}/Views/Shared/{0}.cshtml",
                "~/Areas/Admin/Views/Shared/{0}.cshtml",
            };

            ViewLocationFormats = new[] {
                "~/Views/Shared/Themes/{2}/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml",
            };

            MasterLocationFormats = new[] {
                "~/Views/Shared/Themes/{2}/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml",
            };

            PartialViewLocationFormats = new[] {
                "~/Views/Shared/Themes/{2}/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml",
            };

            ViewStartFileExtensions = new[] { "cshtml" };
#if LOCALIZE
            PageExtensions = new[] {
                                    ".cshtml"
            };
#endif
        }

        public string[] ViewStartFileExtensions { get; set; }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return new RazorView(controllerContext, partialPath, null, false, ViewStartFileExtensions);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            return new RazorView(controllerContext, viewPath, masterPath, true, ViewStartFileExtensions);
        }

        protected override bool IsValidCompiledType(ControllerContext controllerContext, string virtualPath, Type compiledType)
        {
            return typeof(WebViewPage).IsAssignableFrom(compiledType);
        }
    }
}
